<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Begins a database transaction</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.sqlsrv-begin-transaction" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sqlsrv_begin_transaction</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">sqlsrv_begin_transaction</span> &mdash; <span class="dc-title">Begins a database transaction</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.sqlsrv-begin-transaction-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sqlsrv_begin_transaction</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$conn</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   The transaction begun by <span class="function"><strong>sqlsrv_begin_transaction()</strong></span> includes 
   all statements that were executed after the call to 
   <span class="function"><strong>sqlsrv_begin_transaction()</strong></span> and before calls to 
   <span class="function"><a href="sqlsrv_rollback.html" class="function">sqlsrv_rollback()</a></span> or <span class="function"><a href="sqlsrv_commit.html" class="function">sqlsrv_commit()</a></span>. 
   Explicit transactions should be started and committed or rolled back using 
   these functions instead of executing SQL statements that begin and committ/roll 
   back transactions. For more information, see 
   <a href="http://msdn.microsoft.com/en-us/library/cc296206.aspx" class="link external">&raquo;&nbsp;SQLSRV Transactions</a>.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.sqlsrv-begin-transaction-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">conn</code></dt>

     <dd>

      <p class="para">
       The connection resource returned by a call to <span class="function"><a href="sqlsrv_connect.html" class="function">sqlsrv_connect()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-begin-transaction-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.sqlsrv-begin-transaction-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="sqlite3.enableexceptions.example.basic">
    <p><strong>Example #1 <span class="function"><strong>sqlsrv_begin_transaction()</strong></span> example</strong></p>
    <div class="example-contents"><p>
    The following example demonstrates how to use 
    <span class="function"><strong>sqlsrv_begin_transaction()</strong></span> together with 
    <span class="function"><a href="sqlsrv_commit.html" class="function">sqlsrv_commit()</a></span> and <span class="function"><a href="sqlsrv_rollback.html" class="function">sqlsrv_rollback()</a></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"userName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Begin&nbsp;the&nbsp;transaction.&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(&nbsp;</span><span style="color: #0000BB">sqlsrv_begin_transaction</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Initialize&nbsp;parameter&nbsp;values.&nbsp;*/<br /></span><span style="color: #0000BB">$orderId&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$qty&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$productId&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;up&nbsp;and&nbsp;execute&nbsp;the&nbsp;first&nbsp;query.&nbsp;*/<br /></span><span style="color: #0000BB">$sql1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;OrdersTable&nbsp;(ID,&nbsp;Quantity,&nbsp;ProductID)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(?,&nbsp;?,&nbsp;?)"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$params1&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #0000BB">$orderId</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$qty</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$productId&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_query</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params1&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;up&nbsp;and&nbsp;execute&nbsp;the&nbsp;second&nbsp;query.&nbsp;*/<br /></span><span style="color: #0000BB">$sql2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;InventoryTable&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET&nbsp;Quantity&nbsp;=&nbsp;(Quantity&nbsp;-&nbsp;?)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;ProductID&nbsp;=&nbsp;?"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$params2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">$qty</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$productId</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_query</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params2&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;If&nbsp;both&nbsp;queries&nbsp;were&nbsp;successful,&nbsp;commit&nbsp;the&nbsp;transaction.&nbsp;*/<br />/*&nbsp;Otherwise,&nbsp;rollback&nbsp;the&nbsp;transaction.&nbsp;*/<br /></span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">$stmt1&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$stmt2&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sqlsrv_commit</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Transaction&nbsp;committed.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sqlsrv_rollback</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Transaction&nbsp;rolled&nbsp;back.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.sqlsrv-begin-transaction-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="sqlsrv_commit.html" class="function" rel="rdfs-seeAlso">sqlsrv_commit()</a> - Commits a transaction that was begun with sqlsrv_begin_transaction</span></li>
    <li class="member"><span class="function"><a href="sqlsrv_rollback.html" class="function" rel="rdfs-seeAlso">sqlsrv_rollback()</a> - Rolls back a transaction that was begun with 
  sqlsrv_begin_transaction</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>